package com.sservice.commonBusiness.controller.web;

import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.e.Category;
import com.sservice.commonBusiness.mapper.CategoryMapper;
import com.sservice.commonBusiness.service.CategoryService;
import com.sservice.commonBusiness.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * Created by byte_su@163.com on 2018/01/12.
 */
@Controller
@RequestMapping("/category1/")
public class Category1Controller {

    @Autowired
    CategoryService categoryService;

    @Autowired
    CategoryMapper categoryMapper;


    @RequestMapping("listBy")
    @ResponseBody
    public ListResult listBy(Category category) {
        if (category.getType1() == null) return new ListResult(false, "参数错误");
        if ("region".equals(category.getType1())) {
            if ("2".equals(category.getLevel())) {
                if ("110000".equals(category.getRemark())) return getList("110000", "北京市");
                if ("120000".equals(category.getRemark())) return getList("120000", "天津市");
                if ("500000".equals(category.getRemark())) return getList("500000", "重庆市");
                if ("310000".equals(category.getRemark())) return getList("310000", "上海市");
            }
            category.setLevel(null);

        }
        List<Category> list = categoryMapper.selectListBy(category);
        ListResult listResult = new ListResult(true, list);
        return listResult;
    }

    ListResult getList(String code, String city) {
        List<Category> list = new ArrayList<>();
        Category category1 = new Category();
        category1.setName(city);
        category1.setDesc1(code);
        list.add(category1);
        return new ListResult(true, list);
    }

    @RequestMapping("list")

    @ResponseBody
    public ListResult list(CategoryVo categoryVo) {

        List<CategoryVo> list = categoryService.page(categoryVo);
        ListResult listResult = new ListResult(true, list);
        int total = categoryService.queryCount(categoryVo);

        listResult.setPage(new Pagination(categoryVo.getPage(), total, categoryVo.getSize()));
        return listResult;
    }

    @RequestMapping("listDR")
    @ResponseBody
    public ListResult listDR(CategoryVo categoryVo) {
        List<CategoryVo> list = categoryService.queryWithDrByPage(categoryVo);
        ListResult listResult = new ListResult(true, list);
        int total = categoryService.countWithDrByPage(categoryVo);
//        int totalPage = CategoryVo.getTotalPage(total, categoryVo.getSize());
        listResult.setPage(new Pagination(categoryVo.getPage(), total, categoryVo.getSize()));
        return new ListResult(true, listResult);
    }
}
